package com.yiwei.zxp.fragment;

import android.os.Bundle;
import android.text.Editable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yiwei.zxp.R;
import com.yiwei.zxp.activity.SendEvaluateActivity;
import com.yiwei.zxp.adapter.GridViewAddImgesAdpter;
import com.yiwei.zxp.api.model.ItemGoodsDetail;
import com.yiwei.zxp.event.UploadCommentImgEvent;
import com.yiwei.zxp.util.UI;
import com.yiwei.zxp.view.NestedGridView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnTextChanged;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 18-8-1.
 */

public class SendCommentFragment extends BaseFragment {
    @BindView(R.id.img_good)
    ImageView imgGood;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.img_star_one)
    ImageView imgStarOne;
    @BindView(R.id.img_star_two)
    ImageView imgStarTwo;
    @BindView(R.id.img_star_three)
    ImageView imgStarThree;
    @BindView(R.id.img_star_four)
    ImageView imgStarFour;
    @BindView(R.id.img_star_five)
    ImageView imgStarFive;
    @BindView(R.id.edit_shopping_share)
    EditText editShoppingShare;
    @BindView(R.id.upload_gridview)
    NestedGridView uploadGridview;
    @BindView(R.id.cb_check)
    ImageView cbCheck;
    Unbinder unbinder;
    @BindView(R.id.layout_anonym)
    FrameLayout layoutAnonym;
    private List<String> datas;
    private GridViewAddImgesAdpter gridViewAddImgesAdpter;
    private boolean isNiMing = false;
    private int fragmentPoi;
    private List<String> imageData;

    @Override
    protected int setLayoutId() {
        return R.layout.item_one_comment_submit;
    }

    @Override
    protected void initView() {
        EventBus.getDefault().register(this);
        if (getArguments() != null) {
            fragmentPoi = getArguments().getInt("id", 0);
            boolean is_single = getArguments().getBoolean("is_single", false);
            if (is_single) {
                layoutAnonym.setVisibility(View.GONE);
            } else {
                layoutAnonym.setVisibility(View.VISIBLE);
            }
        }
        datas = new ArrayList<>();
        gridViewAddImgesAdpter = new GridViewAddImgesAdpter(datas, getContext());
        uploadGridview.setAdapter(gridViewAddImgesAdpter);
        uploadGridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                if (position == gridViewAddImgesAdpter.getCount() - 1 && position < 6 && !gridViewAddImgesAdpter.isLastUpload()) {
                    ((SendEvaluateActivity) getActivity()).goToTakePhoto(fragmentPoi);
                }
            }
        });

        if (imageData != null && imageData.size() != 0) {
            Glide.with(this).load(imageData.get(0)).placeholder(R.color.image_default).into(imgGood);
        }
    }

    public void setImage(List<String> imageData) {
        this.imageData = imageData;
    }


    @OnTextChanged(R.id.edit_shopping_share)
    void afterTextChanged(Editable s) {
        ((SendEvaluateActivity) getActivity()).getSubmitData().get(fragmentPoi).content = UI.toString(editShoppingShare);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(UploadCommentImgEvent event) {
        if (event.poi == fragmentPoi) {
            savePath(event.path);
        }
    }

    public void savePath(String path) {
//        datas.add(path);
        gridViewAddImgesAdpter.getDatas().add(path);
        gridViewAddImgesAdpter.notifyDataSetChanged();
        ((SendEvaluateActivity) getActivity()).getSubmitData().get(fragmentPoi).photos = gridViewAddImgesAdpter.getDatas();
    }


    public void setStar(int number) {
        ((SendEvaluateActivity) getActivity()).getSubmitData().get(fragmentPoi).score = number + "";
        imgStarOne.setImageResource(R.mipmap.img_star_uncheck);
        imgStarTwo.setImageResource(R.mipmap.img_star_uncheck);
        imgStarThree.setImageResource(R.mipmap.img_star_uncheck);
        imgStarFour.setImageResource(R.mipmap.img_star_uncheck);
        imgStarFive.setImageResource(R.mipmap.img_star_uncheck);
        switch (number) {
            case 0:
                break;
            case 1:
                imgStarOne.setImageResource(R.mipmap.img_star_check);
                break;
            case 2:
                imgStarOne.setImageResource(R.mipmap.img_star_check);
                imgStarTwo.setImageResource(R.mipmap.img_star_check);
                break;
            case 3:
                imgStarOne.setImageResource(R.mipmap.img_star_check);
                imgStarTwo.setImageResource(R.mipmap.img_star_check);
                imgStarThree.setImageResource(R.mipmap.img_star_check);
                break;
            case 4:
                imgStarOne.setImageResource(R.mipmap.img_star_check);
                imgStarTwo.setImageResource(R.mipmap.img_star_check);
                imgStarThree.setImageResource(R.mipmap.img_star_check);
                imgStarFour.setImageResource(R.mipmap.img_star_check);
                break;
            case 5:
                imgStarOne.setImageResource(R.mipmap.img_star_check);
                imgStarTwo.setImageResource(R.mipmap.img_star_check);
                imgStarThree.setImageResource(R.mipmap.img_star_check);
                imgStarFour.setImageResource(R.mipmap.img_star_check);
                imgStarFive.setImageResource(R.mipmap.img_star_check);
                break;
        }
    }

    @OnClick({R.id.cb_check, R.id.img_star_one, R.id.img_star_two, R.id.img_star_three, R.id.img_star_four, R.id.img_star_five})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_star_one:
                setStar(1);
                break;
            case R.id.img_star_two:
                setStar(2);
                break;
            case R.id.img_star_three:
                setStar(3);
                break;
            case R.id.img_star_four:
                setStar(4);
                break;
            case R.id.img_star_five:
                setStar(5);
                break;
            case R.id.cb_check:
                isNiMing = !isNiMing;
                if (isNiMing) {
                    cbCheck.setImageResource(R.mipmap.img_check_box_check);
                    ((SendEvaluateActivity) getActivity()).getSubmitData().get(fragmentPoi).anonym = "1";
                } else {
                    cbCheck.setImageResource(R.mipmap.img_check_box_uncheck);
                    ((SendEvaluateActivity) getActivity()).getSubmitData().get(fragmentPoi).anonym = "0";
                }
                break;
        }
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
        EventBus.getDefault().unregister(this);
    }
}
